using ICSharpCode.TextEditor.Document;
using System;
using System.Collections.Generic;
namespace ICSharpCode.TextEditor.Actions
{
	public class ToggleFolding : AbstractEditAction
	{
		public override void Execute(TextArea textArea)
		{
			List<FoldMarker> list = textArea.Document.FoldingManager.GetFoldingsWithStart(textArea.Caret.Line);
			if (list.Count != 0)
			{
				using (List<FoldMarker>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						FoldMarker current = enumerator.Current;
						current.IsFolded = !current.IsFolded;
					}
					goto IL_EA;
				}
			}
			list = textArea.Document.FoldingManager.GetFoldingsContainsLineNumber(textArea.Caret.Line);
			if (list.Count != 0)
			{
				FoldMarker foldMarker = list[0];
				for (int i = 1; i < list.Count; i++)
				{
					if (new TextLocation(list[i].StartColumn, list[i].StartLine) > new TextLocation(foldMarker.StartColumn, foldMarker.StartLine))
					{
						foldMarker = list[i];
					}
				}
				foldMarker.IsFolded = !foldMarker.IsFolded;
			}
			IL_EA:
			textArea.Document.FoldingManager.NotifyFoldingsChanged(EventArgs.Empty);
		}
	}
}
